package com.itgacl.magic4j.modules.alipay.controller;

import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import com.itgacl.magic4j.common.base.AppBaseController;
import com.itgacl.magic4j.libcommon.bean.R;
import com.itgacl.magic4j.modules.alipay.service.AlipayService;
import com.itgacl.magic4j.modules.alipay.vo.AlipayAccessTokenVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 徐达沛
 * @version 1.0
 * @description: 支付宝开放平台API
 * @date 2021/9/9 15:05
 */
@RestController
@RequestMapping("/app/api/alipay")
public class AlipayApiController extends AppBaseController {

    @Autowired
    private AlipayService alipayService;

    @ApiOperation("获取支付宝开放平台OauthToken")
    @GetMapping("/getOauthToken")
    public R<AlipayAccessTokenVo> getOauthToken(String authCode) {
        AlipayAccessTokenVo accessTokenVo = new AlipayAccessTokenVo();
        AlipaySystemOauthTokenResponse oauthToken = alipayService.getOauthToken(authCode);
        accessTokenVo.setAccessToken(oauthToken.getAccessToken());
        accessTokenVo.setUserId(oauthToken.getUserId());
        return R.ok(accessTokenVo);
    }

    @ApiOperation("获取支付宝开放平台用户信息")
    @GetMapping("/getAlipayUserInfo")
    public R<AlipayUserInfoShareResponse> getAlipayUserInfo(String accessToken) {
        return R.ok(alipayService.getAlipayUserInfo(accessToken));
    }
}
